#include"Quadrant.h"

Quadrant::Quadrant()
{
	m_QState = GREEN;
	m_explored = false;
	m_hasPlayer = false ;
	m_EnemyAttacking = false ;
	m_justWarped = false ;

	//create all empty sectors
	for( int i = 0; i < 8; i++ )
		for( int j = 0; j < 8; j++ )
			sector[i][j] = EMPTY ;

	srand( unsigned( time(NULL) ) ) ;

	//create random number
	int rnd = (rand() % 100) + 1 ;

	//add number of enemies
	if( rnd > 20 )
	{
		m_lrsInfo.m_numEnemies = 0 ;
	}
	else
	{
		//change status of quadrant to RED
		m_QState = RED ;

		if( rnd <= 2 )
			m_lrsInfo.m_numEnemies = 3 ;
		else if( rnd <= 5 )
			m_lrsInfo.m_numEnemies = 2 ;
		else if( rnd <= 20 )
			m_lrsInfo.m_numEnemies = 1 ;
	}

	//create new enemies
	for( int i = 0; i < m_lrsInfo.m_numEnemies; i++ )
		m_enemies[i] = new Enemy( ) ;

	//recreate random number
	rnd = (rand() % 100) + 1 ;

	//add station if at 4% or lower
	if( rnd <= 4 )
	{
		m_lrsInfo.m_station = true ;

	}
	else
		m_lrsInfo.m_station = false ;

	//create random number of stars
	m_lrsInfo.m_numStars = rand( ) % 8 ;

	RandomizeQuadrant( ) ;
}

void Quadrant::UpdateEnemy( )
{
	//for random generated numbers
	int rnd ;

	//for all enemies in quadrant
	for( int i = 0; i < m_lrsInfo.m_numEnemies; i++ )
	{
		//skip enemy if they are no longer alive
		if( m_enemies[i] != NULL )
		{
			//if enemy is in combat mode, move them
			if( !m_enemies[i]->GetMode( ) )
			{
				m_enemies[i]->MoveEnemy( ) ;

				//keep moving enemy until they find a sector that is EMPTY
				while( sector[m_enemies[i]->GetPosX()][m_enemies[i]->GetPosY()] != EMPTY )
				{
					m_enemies[i]->MoveEnemy( ) ;
				}

				//change sector to ENEMY
				sector[m_enemies[i]->GetPosX()][m_enemies[i]->GetPosY()] = ENEMY ;

				if( m_hasPlayer && !m_justWarped )
				{
					rnd = rand() % 4 + 1 ;

					if( rnd <= 3 )
					{
						m_enemies[i]->ChangeMode( ) ;
						cout << "An enemy is going into combat mode" << endl ;
					}
					else
						cout << "An enemy is just sitting there..." << endl ;
				}
				else
					m_justWarped = false ;
			}
			else
			{
				rnd = rand() % 2 ;

				//move if 0
				if( rnd == 0 )
				{
					m_enemies[i]->MoveEnemy( ) ;

					//keep moving enemy until they find a sector that is EMPTY
					while( sector[m_enemies[i]->GetPosX()][m_enemies[i]->GetPosY()] != EMPTY )
					{
						m_enemies[i]->MoveEnemy( ) ;
						cout << "Enemy is on the move!" << endl ;
					}

					//change sector to ENEMY
					sector[m_enemies[i]->GetPosX()][m_enemies[i]->GetPosY()] = ENEMY ;
				}
				//attack if 1
				else
				{
					m_EnemyAttacking = true ;
					cout << "Enemy is about to attack!" << endl ;
				}
			}
		}
	}
}

void Quadrant::DamageEnemies( int energy, int b_PosX, int b_PosY )
{
	//distance variable for x and y
	int distanceX, distanceY ;
	for( int i = 0; i < m_lrsInfo.m_numEnemies; i++ )
	{
		//distance between x pos of enemy and player
		distanceX = b_PosX - m_enemies[i]->GetPosX() ;
		//keep positive
		if( distanceX < 0 )
			distanceX = -distanceX ;

		//distance between y pos of enemy and player
		distanceY = b_PosY - m_enemies[i]->GetPosY() ;
		//keep positive
		if( distanceY < 0 )
			distanceY = -distanceY ;

		//take largest distance to use for damage calculations
		if( distanceX >= distanceY )
			m_enemies[i]->DamageEnemy( ((energy / m_lrsInfo.m_numEnemies) / distanceX) * ((rand() % 2) + 2) ) ;
		else
			m_enemies[i]->DamageEnemy( ((energy / m_lrsInfo.m_numEnemies) / distanceY) * ((rand() % 2) + 2) ) ;
	}
}

//when player enters a sector, the quadrant is randomized
void Quadrant::RandomizeQuadrant( )
{
	//for RNG
	int randX, randY ;

	//reset all sectors
	for( int i = 0; i < 8; i++ )
		for( int j = 0; j < 8; j++ )
			sector[i][j] = EMPTY ;

	//set player in first
	sector[rand() % 8][rand() % 8] = PLAYER ;

	//place all enemies in empty sectors
	for( int i = 0; i < m_lrsInfo.m_numEnemies; i++ )
	{
		randX = rand() % 8 ;
		randY = rand() % 8 ;

		//keep looking for an empty sector
		while( sector[randX][randY] != EMPTY )
		{
			randX = rand() % 8 ;
			randY = rand() % 8 ;
		}
			
		sector[randX][randY] = ENEMY ;
		//place enemy in their respective x and y positions
		m_enemies[i]->PlaceEnemy( randX, randY ) ;
	}

	//place station in empty sector
	if( m_lrsInfo.m_station )
	{
		randX = rand() % 8 ;
		randY = rand() % 8 ;

		while( sector[randX][randY] != EMPTY )
		{
			randX = rand() % 8 ;
			randY = rand() % 8 ;
		}
		sector[randX][randY] = STATION ;
	}

	//place stars in empty sector
	if( m_lrsInfo.m_numStars > 0 )
		for( int i = 0; i < m_lrsInfo.m_numStars; i++ )
		{
			randX = rand() % 8 ;
			randY = rand() % 8 ;

			while( sector[randX][randY] != EMPTY )
			{
				randX = rand() % 8 ;
				randY = rand() % 8 ;
			}
			sector[randX][randY] = STAR ;
		}
}

void Quadrant::PlayerExist( )
{
	if( !m_hasPlayer )
	{
		m_justWarped = true ;
		m_hasPlayer = true ;
	}
	else
	{
		m_justWarped = false ;
		m_hasPlayer = false ;
	}
}

void Quadrant::DestroyEnemy( bool isTarget, int dist, int x, int y )
{
	int probability = rand() % 99 + 1 ;

	if( (isTarget && probability <= (dist/(dist*dist) )) || probability <= 50 )
		delete FindEnemy( x, y ) ;

	EmptySector( x, y ) ;
}

bool Quadrant::EnemyIsAttacking( )
{
	if( m_EnemyAttacking )
	{
		m_EnemyAttacking = false ;
		return true ;
	}
	else
		return false ;
}

Enemy* Quadrant::FindEnemy( int x, int y )
{
	for( int i = 0; i < m_lrsInfo.m_numEnemies; i++ )
	{
		if( m_enemies[i] != NULL )
			if( m_enemies[i]->GetPosX() == x && m_enemies[i]->GetPosY() == y )
				return m_enemies[i] ;
	}
}